
(function(){
	$.disform=function($status){
		var $obj=$("#btn-confirm");
		$obj.attr("disabled",$status);
		if($status){
			$obj.addClass("weui-btn_disabled");
		}else{
			$obj.removeClass("weui-btn_disabled");
		
		}
	};
	//计算总价
	$.calcTotal=function(){
		
		var $total=$("#total").attr("data-total");
		$total=getfloat($total);
		
		//优惠券
		var $couponamount=$("input[name='couponno']:checked").attr("data-amount"); 
		$couponamount=getfloat($couponamount);
		if($couponamount>0){ 
			$("#couponbox").show();
		}else{ 
			$("#couponbox").hide(); 
		} 
		$("#couponamount").text($couponamount);
		$total-=$couponamount;
		
		//余额
		var $balance1=getfloat($("#balance1").val()); 
		var $balance1max=getfloat($("#balance1").attr("data-max")); 
		if($balance1>$balance1max){
			alerterr('使用余额数不能超过'+$balance1max);
			$.disform(true);
			return false;
		} 
		if($balance1>$total){
			$balance1=$total;
			$("#balance1").val($balance1);
		}
		if($balance1>0){ 
			$("#balance1").val($balance1.toFixed(2));
			$("#balance1box").show();
		}else{ 
			$("#balance1box").hide(); 
		} 
		$("#balance1amount").text($balance1.toFixed(2));
		$total-=$balance1;
	 	
		//积分
		var $balance2=getfloat($("#balance2").val());
		var $rate=getfloat($("#balance2").attr("data-rate"));
		var $balance2max=getfloat($("#balance1").attr("data-max")); 
		if($balance2>$balance2max){
			alerterr('使用积分数不能超过'+$balance2max);
			$.disform(true);
			return false;
		}
		
		if($balance2>0){ 
			$("#balance2box").show();
		}else{ 
			$("#balance2box").hide(); 
		} 
		var $balance2amount=getfloat($balance2*$rate);
		$("#balance2amount").text($balance2amount.toFixed(2));
		$total-=$balance2amount;
		
		if($total<0){
			alerterr("抵扣过多");
			$.disform(true);
			return false;
			$total=0;
		}
		$.disform(false);
		$("#total,#total1").text($total.toFixed(2));
	};
	
	$.setNum=function($num){
		location=$pin_url+"?num="+$num;
	}
})(jQuery);
$(function(){
	//加
	$("body").on("change", ".cartnum-pin", function () { 
		var $v=getnum($(this).val()); 
		$.setNum($v);  
	});
	//加
	$("body").on("click", ".btn-plus-pin", function () {
		var $obj = $(this).prev(".cartnum-pin");
		var $v=getnum($obj.val());
		$v++; 
		$.setNum($v);  
	});
	
	//减
	$("body").on("click", ".btn-minus-pin", function () {
		var $obj = $(this).next(".cartnum-pin");
		var $v=getnum($obj.val());
		$v--;
		if($v<1){
			$v=1;
		}
		$.setNum($v);
	});
	
	$("#btn-confirm").click(function(){  
		$.disform(true);
		weui.confirm('您确定提交订单吗？', function(){ 
			$("#form1").submit();
			$.disform(false);
		},function(){
			$.disform(false);
		});
		 
	});
	
	$(".ajaxformx1").ajaxForm({
		beforeSubmit:function(d,f,o){ 
			var btnSubmit=f.find("button[type='submit']");
			btnSubmit.attr("disabled",true); 		
		},success:
		function(data){			
			if(data.code!=0){
				var url="/pay/order/orderno/"+data.msg+'.html';
				var $paymethod=$("input[name='paymethod']:checked").val(); 
				alertok('订单提交成功');
				if(url==""||url==undefined){
					location.reload();
				}else{
					location=url;	
				}
			}else{
				alerterr(data.msg);
			}
			$("button[type='submit']").attr("disabled",false);
		}
	});
	
	 
	//选择地址
	$("#selectadr,#btn-addr-cancel").on("click",function(){
		$(".selectadr").toggleClass("hide");
		$("#jiesuanmask").fadeToggle("fast");
		$.calcTotal();
	});
	$("#btn-addr-confirm").on("click",function(){
		var $select=$("input[name='addressid']:checked");
		var $html=$select.parent().prev(".weui-cell__bd").html();
		$("#addressbox").html($html);
		$("#btn-addr-cancel").click();
	});
	//选择优惠券
	$("#selectquan").on("click",function(){
		$(".selectquan").toggleClass("hide");
		$("#jiesuanmask").fadeToggle("fast");
		$.calcTotal();
	});
	$("#btn-quan-cancel").on("click",function(){
		$("#selectquan").click();
	});
	$("#btn-quan-confirm").on("click",function(){
		
		$("#selectquan").click();
		var $select=$("input[name='couponno']:checked");
		var $html=$select.attr("data-amount");
		var $text=$("#coupontext").attr("data-num");
		if(parseFloat($html)>0){
			 $text="-"+$html;
		}
		$("#coupontext").text($text); 
		
	});
	//余额抵扣
	$("#usebalance1").on("change",function(){
		if($(this).prop("checked")){
			$("#balance1input").removeClass("weui-hidden")
		}else{
			$("#balance1input").addClass("weui-hidden");
			$("#balance1").val('');
		}
		$.calcTotal();
	})
	//积分抵扣
	$("#usebalance2").on("change",function(){
		if($(this).prop("checked")){
			$("#balance2input").removeClass("weui-hidden")
		}else{
			$("#balance2input").addClass("weui-hidden");
			$("#balance2").val('');
		}
		$.calcTotal();
	});
	$("#balance1,#balance2").change(function(){
		$.calcTotal();
	});
	
	$.calcTotal();
});